package com.partner.frame.support.util;

import java.text.*;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.*;

public class DateConvertUtil
{

   public static java.util.Date parse(String dateString, String dateFormat)
   {
      return parse(dateString, dateFormat, java.util.Date.class);
   }

   @SuppressWarnings("unchecked")
   public static <T extends java.util.Date> T parse(String dateString, String dateFormat, Class<T> targetType)
   {
      long lTime = 0;
      String errorInfo = "";
      DateFormat fmtDate = null;
      java.util.Date dateResult = null;

      if(StringUtils.isEmpty(dateString)) return null;

      fmtDate = new SimpleDateFormat(dateFormat);
      try
      {
         lTime = fmtDate.parse(dateString).getTime();
         dateResult = targetType.getConstructor(long.class).newInstance(lTime);
      }
      catch(ParseException eParse)
      {
         errorInfo = "dateformat[" + dateFormat + "] datestring[" + dateString + "]";
         throw new IllegalArgumentException(errorInfo, eParse);
      }
      catch(Exception eOther)
      {
         throw new IllegalArgumentException("error_type[" + targetType.getName() + "]", eOther);
      }

      return (T) dateResult;
   }

   public static String format(java.util.Date objDate, String dateFormat)
   {
      if(objDate == null) return null;
      
      return new SimpleDateFormat(dateFormat).format(objDate);
   }
   //返回相隔天数 
   public static long getDatePoor(Date endDate, Date nowDate) {
	   
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    return day ;
	}
   public static int differentDaysByMillisecond(Date bigdate,Date sdate)
   {
       int days = (int) ((sdate.getTime() - bigdate.getTime()) / (1000*3600*24));
       return days;
   }
   //返回相隔分钟数 
   public static long getMinPoor(Date endDate, Date nowDate) {
	   
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    return min ;
	}
   /* 
    * 将时间戳转换为时间
    */
   public static String stampToDate(String s){
       String res;
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       long lt = new Long(s);
       Date date = new Date(lt);
       res = simpleDateFormat.format(date);
       return res;
   }
   /* 
    * 将时间转换为时间戳
    */    
   public static String dateToStamp(String s) throws ParseException{
       String res;
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date = simpleDateFormat.parse(s);
       long ts = date.getTime();
       res = String.valueOf(ts);
       return res;
   }
   /**
    * 返回N天前日期
    * @param d
    * @param day
    * @return
    */
   public static Date getDateBefore(Date d,int day){
	     Calendar now =Calendar.getInstance();
	     now.setTime(d);
	     now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
	     return now.getTime();
	 }
   
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
		 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	       Date date = simpleDateFormat.parse("2018-09-05 00:00:00");
			DateConvertUtil s = new DateConvertUtil();
			int i = s.differentDaysByMillisecond(date,  new Date());
			System.out.println(i);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		
	}
}
